﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using WHMS.EntityClasses;
using WMS.HelperClasses;

namespace WMS.Reports
{
    public partial class SupplyOrderReportForm : Form
    {
        public SupplyOrderEntity currentSupplyOrder;
        public List<SupplyOrderItemEntity> currentSupplyOrderItems;
        public SupplyOrderReportForm(SupplyOrderEntity supplyOrder, List<SupplyOrderItemEntity> supplyOrderItems)
        {
            currentSupplyOrder = supplyOrder;
            currentSupplyOrderItems = supplyOrderItems;

            InitializeComponent();
        }

        private void SupplyOrderForm_Load(object sender, EventArgs e)
        {

            DataTable DTSupplyOrderItems = new DataTable();
            DTSupplyOrderItems.Columns.Add(new DataColumn("Sequence"));
            DTSupplyOrderItems.Columns.Add(new DataColumn("Name"));
            DTSupplyOrderItems.Columns.Add(new DataColumn("Unit"));
            DTSupplyOrderItems.Columns.Add(new DataColumn("Quantity"));
            DTSupplyOrderItems.Columns.Add(new DataColumn("UnitPrice"));
            DTSupplyOrderItems.Columns.Add(new DataColumn("Total"));

            int sequence = 0;
            double totalInitially = 0;
            foreach (SupplyOrderItemEntity supplyOrderItem in currentSupplyOrderItems)
            {
                sequence++;
                DataTable DTItem = Business.GetItemDetails(supplyOrderItem.ItemId);

                double total = (double)supplyOrderItem.Quantity * (double)supplyOrderItem.UnitPrice;
                DTSupplyOrderItems.Rows.Add(sequence, DTItem.Rows[0][1].ToString(),DTItem.Rows[0]["Unit"].ToString(),supplyOrderItem.Quantity,supplyOrderItem.UnitPrice,total.ToString());
                totalInitially += total;
            }

            bindingSource1.DataSource = DTSupplyOrderItems;
            double totalAfterDiscount = totalInitially;

            double totalAfterTransportation = totalInitially;

            double taxSale = 0.1 * totalAfterTransportation;

            double halftax = 0.05 * taxSale;

            double totalNet = totalAfterTransportation + taxSale + halftax;

            string totalNetArabic = NumericToArabic.NtoS(totalNet.ToString());
            totalNetArabic += " فقط لا غير";

            //SupplyOrderItemEntityBindingSource.DataSource = DTSupplyOrderItems;//Business.GetStoreBalance(selectedStoreID, dtpStoreEndDate.Value);
            //var storeCode = (string)DALHelper.GetValue(StoreFields.Code, StoreFields.Id, selectedStoreID);


            this.reportViewer1.LocalReport.DataSources.Clear();
            this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", DTSupplyOrderItems));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("TotalInitially", totalInitially.ToString()));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("TotalAfterDiscount", totalAfterDiscount.ToString()));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("TotalAfterTransportation", totalAfterTransportation.ToString()));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("SalesTax", taxSale.ToString()));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("HalfTax", halftax.ToString()));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("TotalNet", totalNet.ToString()));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("TotalNetArabic", totalNetArabic.ToString()));

            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("SupplyOrderNumber", currentSupplyOrder.SupplyOrderNumber.ToString()));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("SupplyOrderDate", currentSupplyOrder.CreatedOn.ToString()));
            //this.reportViewer1.LocalReport.SetParameters(new ReportParameter("StoreCode", storeCode));
            //this.reportViewer1.LocalReport.SetParameters(new ReportParameter("StoreEndDate", dtpStoreEndDate.Value.ToString()));

            this.reportViewer1.RefreshReport();	

        }
    }
}
